package StudentManage;

import java.util.*;

/**
 * @Auther: MFM
 * @Date: 2021/7/24 - 07 - 24 - 14:35
 * @version: 1.0
 */

public class StudentManageDemo {
    public static class Student {
        private String name;
        private String gender;
        private int age;
        private int cardId;
        private String address;

        public Student(String name, String gender, int age, String address, int cardId) {
            this.name = name;
            this.gender = gender;
            this.age = age;
            this.address = address;
            this.cardId = cardId;
        }

        public Student() {
        }

        public int getCardId() {
            return cardId;
        }

        public void setCardId(int cardId) {
            this.cardId = cardId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", gender='" + gender + '\'' +
                    ", age=" + age +
                    ", cardId=" + cardId +
                    ", address='" + address + '\'' +
                    '}';
        }
    }

    private static int cardId=10000;

    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        List<Student> studList = new ArrayList<>();
        boolean isRun = true;

        System.out.println("欢迎使用学生管理系统1.0");
        while (isRun) {
            System.out.println("请选择操作：1、录入 2、查询 3、修改 4、删除 5、退出");
            String operate = scan.next();
            switch (operate) {
                case "1":
                    addStudent(studList, scan);
                    break;
                case "2":
                    queryStudent(studList, scan);
                    break;
                case "3":
                    changeStudentInformation(studList, scan);
                    break;
                case "4":
                    deleteStudent(studList,scan);
                    break;
                case "5":
                    isRun = false;
                    break;
            }
        }
    }

    private static void deleteStudent(List<Student> list, Scanner scan) {
        System.out.println("请输入学生学号");
        int cardId = Integer.parseInt(scan.next());
        boolean flag = false;
        Iterator<Student> iter = list.listIterator();
        while (iter.hasNext()){
            Student st = iter.next();
            if (st.getCardId() == cardId){
                iter.remove();
                flag= true;
                break;
            }
        }
        if (flag){
            System.out.println("已将该学生删除!");
        }else {
            System.out.println("没有查到该学生信息");
        }
    }

    private static void changeStudentInformation(List<Student> list, Scanner scan) {
        System.out.println("请输入学生学号");
        int cardId = Integer.parseInt(scan.next());
        Student stud = null;
        for (Student student : list) {
            if (student.getCardId() == cardId){
                stud = student;
                break;
            }
        }
        if (stud==null){
            System.out.println("没有查到该学生信息");
        }else {
            System.out.println("学生姓名："+stud.getName()+"，是否修改？Y/N");
            String operate = scan.next();
            if ("Y".equalsIgnoreCase(operate)){
                System.out.println("请输入学生姓名");
                String name = scan.next();
                stud.setName(name);
            }
            System.out.println("学生年龄："+stud.getAge()+"，是否修改？Y/N");
            operate = scan.next();
            if ("Y".equalsIgnoreCase(operate)){
                System.out.println("请输入学生年龄");
                int age = Integer.parseInt(scan.next());
                stud.setAge(age);
            }
            System.out.println("学生信息修改完毕");
            System.out.println(stud);
        }
    }

    private static void queryStudent(List<Student> list, Scanner scan) {
        System.out.println("请选择操作：1、查询所有学生 2、根据姓名进行查询");
        int operate = Integer.parseInt(scan.next());
        if (operate == 1) {
            list.forEach(System.out::println);
        } else if (operate == 2) {
            System.out.println("请输入学生姓名,可进行模糊查询");
            String name = scan.next();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getName().startsWith(name)) {
                    System.out.println(list.get(i));
                }
            }
        } else {
            System.out.println("选择失败 退出查询功能");
        }
    }

    private static void addStudent(List<Student> list, Scanner scan) {
        String operate = "Y";
        while ("Y".equalsIgnoreCase(operate)) {
            System.out.println("请输入学生的姓名");
            String name = scan.next();
            System.out.println("请输入学生性别");
            String gender = scan.next();
            System.out.println("请输入学生的年龄");
            int age = Integer.parseInt(scan.next());
            System.out.println("请输入学生的住址");
            String address = scan.next();
            Student stud = new Student(name, gender, age, address,cardId++);
            list.add(stud);
            System.out.println("学生添加成功！是否继续录入？Y/N");
            operate = scan.next();
        }

    }
}
